package game.controller.Region;

import lobby.controller.Nation;
import game.controller.Unit.MilitaryUnit;
import game.util.GameMsg;

import java.util.ArrayList;

/**
 * Region.java  Date Created: Nov 8, 2012
 *
 * Purpose: To keep track of the details of a region.
 *
 * Description: name, isHomeland, controllingNation, productionValue,
 *
 * @author Chrisb
 */
public class LandRegion extends Region {
    public LandRegion(String regionName, boolean isHomeland, int controllingNation, boolean onCoast, boolean hasPort, boolean isAfrica) {
        super(regionName);
        this.isHomeland = isHomeland;
        this.controllingNation = controllingNation;
        this.hasPort = hasPort;
        this.onCoast = onCoast;
        this.isAfrica = isAfrica;
        uprising = false;
        type = LAND_REGION;
    }

    public LandRegion(String regionName, boolean isHomeland, boolean onCoast, boolean hasPort, boolean isAfrica) {
        super(regionName);
        this.isHomeland = isHomeland;
        this.controllingNation = Nation.NEUTRAL;
        this.hasPort = hasPort;
        this.onCoast = onCoast;
        this.isAfrica = isAfrica;
        uprising = false;
        type = LAND_REGION;
    }

    public ArrayList<MilitaryUnit> getNationsUnits(int nation) {
        ArrayList<MilitaryUnit> nationsUnits = new ArrayList<MilitaryUnit>();

        for (MilitaryUnit unit : occupyingUnits) {
            if (unit.getOwningNation() == nation) {
                nationsUnits.ensureCapacity(nationsUnits.size() + 1);
                nationsUnits.add(unit);
            }
        }
        return nationsUnits;
    }

    public int getControllingNation() { return controllingNation; }
    public boolean isUprising() { return uprising; }

    public boolean isEnemyOccupied() {
        if (isOccupied())
            for ( MilitaryUnit u : getOccupyingUnits() )
                if (u.getOwningNation() != getControllingNation() )
                    return true;

        return false;
    }

    public void setControllingNation(int nation) { controllingNation = nation; }
    public void setUprising(boolean uprising) { this.uprising = uprising; }

    public boolean isHomeland() { return isHomeland; }
    public boolean isOnCoast() { return onCoast; }
    public boolean isAfrica() { return isAfrica; }

    public int getProductionValue() {
        if (uprising || isEnemyOccupied())
            return 0;
        else if (controllingNation == Nation.OTTOMANS)
            return ottomanProductionValue;
        else if (isAfrica && !regionName.equals(GameMsg.getString("Egypt")))
            return africanProductionValue;
        else
            return fullProductionValue;
    }

    protected boolean isHomeland;
    protected int controllingNation;
    protected boolean uprising;
    protected boolean hasPort;
    protected boolean onCoast;
    protected boolean isAfrica;

    protected static final int ottomanProductionValue = 1;
    protected static final int africanProductionValue = 1;
    protected static final int fullProductionValue = 2;
}